package adapter;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Color;
import android.os.Bundle;
import android.support.design.widget.BottomSheetBehavior;
import android.support.design.widget.BottomSheetDialog;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;



import org.greenrobot.eventbus.EventBus;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import base.BaseObserver;
import bean.SchoolPostCommentListResultBean;
import jiyun.com.yiyanmeng.R;
import jiyun.com.yiyanmeng.http.HttpManager;
import jiyun.com.yiyanmeng.http.MySerivce;
import utils.ProgressDialogs;
import utils.RxUtil;
import utils.ToastUtil;


public class MyPostReplyBasedaapter extends BaseAdapter {

    private Context context;
    private List<SchoolPostCommentListResultBean.SchoolPostReplyBean> list = new ArrayList<>();
    private BottomSheetDialog dialog;
    private String replyPath;

    public void setType(int type) {
        this.type = type;
        if (type == 3) {
            replyPath = "forumsc/add_ping";
        } else if (type == 1) {
            replyPath = "forum/add_ping";
        } else {
            replyPath = "forumjy/add_ping";
        }
    }

    private int type;//1 官方论坛详情评论  2 经验论坛详情评论 3学校评论列表点赞


    public void setBean(SchoolPostCommentListResultBean.SchoolPostCommentBean bean) {
        this.bean = bean;
    }

    private SchoolPostCommentListResultBean.SchoolPostCommentBean bean;

    public MyPostReplyBasedaapter(Context context) {
        this.context = context;
    }


    public List<SchoolPostCommentListResultBean.SchoolPostReplyBean> getList() {
        return list;
    }

    public void setList(List<SchoolPostCommentListResultBean.SchoolPostReplyBean> list) {
        this.list = list;
    }

    @Override
    public int getCount() {
        if (bean.reply == null) {
            return 0;
        }
        if (TextUtils.isEmpty(bean.pingNum)) {
            return 0;
        }
        if (Integer.parseInt(bean.pingNum) > 2) {
            return 3;
        }
        return bean.reply.size();
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    private int dipToPixels(int dip) {
        Resources r = context.getResources();
        float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dip, r.getDisplayMetrics());
        return (int) px;
    }

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        ViewHoler holder;
        if (view == null) {
            holder = new ViewHoler();
            view = LayoutInflater.from(context).inflate(R.layout.item_my_post_reply, viewGroup, false);
            view.setPadding(dipToPixels(6),dipToPixels(6),dipToPixels(6),dipToPixels(6));
            holder.tv_reply = view.findViewById(R.id.tv_reply);
            view.setTag(holder);
        } else {
            holder = (ViewHoler) view.getTag();
        }
        if (bean != null && bean.reply.get(i).mUser != null && bean.reply.get(i).toUser != null) {
//            if (!TextUtils.isEmpty(bean.reply.get(i).mUser.userName)&&!TextUtils.isEmpty(bean.reply.get(i).toUser.userName))
//            {
            if (TextUtils.isEmpty(bean.reply.get(i).mUser.userName)) {
                bean.reply.get(i).mUser.userName = "";
            }
            if (TextUtils.isEmpty(bean.reply.get(i).toUser.userName)) {
                bean.reply.get(i).toUser.userName = "";
            }
            holder.tv_reply.setText(bean.reply.get(i).mUser.userName + "回复" + bean.reply.get(i).toUser.userName + "：" + bean.reply.get(i).text);
            SpannableString spannableString = new SpannableString(holder.tv_reply.getText().toString());
            spannableString.setSpan(new ForegroundColorSpan(Color.parseColor("#55A8DE")), 0, bean.reply.get(i).mUser.userName.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            spannableString.setSpan(new ForegroundColorSpan(Color.parseColor("#55A8DE")), bean.reply.get(i).mUser.userName.length() + 2, bean.reply.get(i).mUser.userName.length() + 2 + bean.reply.get(i).toUser.userName.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            holder.tv_reply.setText(spannableString);
//            }

        }
        holder.tv_reply.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                if (holder.tv_reply.getText().toString().equals("查看全部评论")) {
//                    Bundle bundle = new Bundle();
//                    bundle.putString("id", bean.commenId);
//                    bundle.putString("createTime", bean.createTime);
//                    bundle.putString("text", bean.text);
//                    bundle.putString("userName", bean.mUser.userName);
//                    bundle.putString("img", bean.mUser.img);
//                    bundle.putInt("type", type);
//                    MyApplication.openActivity(context, SchoolAllReplyActivity.class, bundle);

//                } else {
                    showCommentDialog(bean.reply.get(i));
//                }

            }
        });
        if (i == 2) {
            holder.tv_reply.setText("查看全部评论");
        }
        return view;
    }

    private void showCommentDialog(final SchoolPostCommentListResultBean.SchoolPostReplyBean bean) {
        dialog = new BottomSheetDialog(context);
        View commentView = LayoutInflater.from(context).inflate(R.layout.layout_comment_dialog, null);
        final EditText commentText = (EditText) commentView.findViewById(R.id.edit_content);
        final TextView bt_comment = (TextView) commentView.findViewById(R.id.tv_finish);
        dialog.setContentView(commentView);
        View parent = (View) commentView.getParent();
        BottomSheetBehavior behavior = BottomSheetBehavior.from(parent);
        commentView.measure(0, 0);
        behavior.setPeekHeight(commentView.getMeasuredHeight());

        bt_comment.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                String commentContent = commentText.getText().toString().trim();
                if (!TextUtils.isEmpty(commentContent)) {
                    submit(bean, commentContent);
                } else {
                    Toast.makeText(context, "评论内容不能为空", Toast.LENGTH_SHORT).show();
                }
            }
        });
        dialog.show();
    }


    private void submit(SchoolPostCommentListResultBean.SchoolPostReplyBean bean, String info) {
        ProgressDialogs.showProgressDialog(context);
        MySerivce serivce = HttpManager.getInstance().getSerivce(MySerivce.base_URL + replyPath, MySerivce.class);
        serivce.pinbean7(bean.commenId,info).compose(RxUtil.<SchoolPostCommentListResultBean>changeThread())
                .subscribe(new BaseObserver <SchoolPostCommentListResultBean>() {
                    @Override
                    public void onSuccess(SchoolPostCommentListResultBean bean) {
                        if (bean.ret.equals("200")){
                            dialog.dismiss();
                        }else{
                            ToastUtil.show(context,bean.mas);
                        }
                    }

                    @Override
                    public void onFail(String error) {

                    }
                });
    }

    private class ViewHoler {
        TextView tv_reply;
    }


}
